package com.liuhouse1.controller;

import com.liuhouse1.pojo.Cat;
import com.liuhouse1.pojo.Dog;
import com.liuhouse1.pojo.Student;
import com.liuhouse1.pojo.Zoo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/tpl")
public class ThymeleafController {
    @RequestMapping("/demo")
    public String demo(HttpServletRequest request){
        //给请求域中设置数据，可以给模版使用
        request.setAttribute("name" , "江湖儿女");
        request.setAttribute("mingyan" , "有的人死了,他还活着,有的人活着,他已经死了");
        //设置模版文件的名称
        return "demo";
    }


    //第一个标准变量表达式
    //https://baijiahao.baidu.com/s?id=1767193047641895388&wfr=spider&for=pc
    @GetMapping("/expression01")
    public String expression01(Model model){
        //添加数据到model,设置到请求域中
        model.addAttribute("site" , "www.beijing.com");
        model.addAttribute("student" , new Student(1 , "张三" , "男" , 23));
        //指定视图
        return "expression01";
    }

    //选择标准变量表达式 , 原理性的东西
    @GetMapping("/expression02")
    public String expression02(Model model){
        //添加数据到Model
        //原型性的东西  通过百度去理解  但是会用参数最重要的  对于原理 不需要每个都看懂 但是心里要知道有这么回事
        model.addAttribute("site" , "www.baijing.cn");
        model.addAttribute("student" , new Student(2,"伍佰" ,"男" , 24));
        //指定视图
        return "expression02";
    }


    @GetMapping("/link")
    public String link(Model model){
        model.addAttribute("userId" , 1002);
        return "link";
    }


    //测试链接表达式的地址
    @GetMapping("/queryAccount")
    @ResponseBody
    public String queryAccount(Integer id){
        return "queryAccount，参数id=" + id;
    }


    //有两个参数的地址
    @GetMapping("/queryUser")
    @ResponseBody
    public String queryUser(String name , Integer age){
        return "queryUser,有两个参数:name=" + name + ",age=" + age;
    }

    //使用模版的属性
    @GetMapping("/property")
    public String userProperty(Model model){
        model.addAttribute("methodAttr" , "post");
        model.addAttribute("id" , "2333");
        model.addAttribute("paramname" , "name");
        model.addAttribute("uservalue" , "lisi");
        model.addAttribute("textcolor" , "color:red");
        return "html-property";
    }

    @RequestMapping("/loginServlet")
    @ResponseBody
    public String loginServlet(){
        return "用户进行了登录操作";
    }

    @GetMapping("/eachList")
    public String eachList(Model model){
        //定义一个字符串数组
       ArrayList strList = new ArrayList<String>();
       //给数组中添加元素
        strList.add("三国");
        strList.add("三国志");
        strList.add("水浒");
        model.addAttribute("strList" , strList);

        //定义一个Student类型的数组
        ArrayList<Student> studentList = new ArrayList<>();
        //给数组中添加元素
        studentList.add(new Student(1001 , "张飞" , "男" , 20));
        studentList.add(new Student(1002 , "刘备" , "男" , 29));
        studentList.add(new Student(1003 , "关羽" , "男" , 22));
        model.addAttribute("studentList" , studentList);
        return "each-list";
    }


    //循环数组
    @GetMapping("/eachArray")
    public String eachArray(Model model) {
        //给请求与中加入数据
        //定义对象类型的数组 , 数组的长度为3  javaSe
        Student[] studentArray = new Student[3];
        studentArray[0] = new Student(1001, "马超", "男", 30);
        studentArray[1] = new Student(1002, "黄忠", "男", 31);
        studentArray[2] = new Student(1003, "赵云", "男", 32);
        //将数据加入到请求域中
        model.addAttribute("studentArray", studentArray);
        //返回视图名称
        return "eachArray";
    }


    @GetMapping("/eachMap")
    public String eachMap(Model model){
        HashMap<String, Student> maps = new HashMap<>();
        maps.put("stu1" , new Student(1001 , "马超" , "男" , 30));
        maps.put("stu2" , new Student(1002 , "赵云" , "男" , 31));
        maps.put("stu3" , new Student(1003 , "张飞" , "男" , 32));
        //给请求域中写入数据
        model.addAttribute("maps" , maps);

        //普通字符串类型的map
        HashMap<String, String> stringMap = new HashMap<>();
        stringMap.put("str1" , "Hello springBoot");
        stringMap.put("str2" , "MyBastis Mysql");
        //给请求域中写入数据
        model.addAttribute("stringMap" , stringMap);


        HashMap<String, Student> maps1 = new HashMap<>();
        maps1.put("stu11" , new Student(10011 , "马超1" , "男" , 301));
        maps1.put("stu22" , new Student(10022 , "赵云2" , "男" , 312));
        maps1.put("stu33" , new Student(10033 , "张飞3" , "男" , 323));


        //复杂的List<Map>  定义一个数组  数组中的每个元素都是一个map元素 其实就相当于是二维数组
        List<Map<String , Student>> list_map = new ArrayList<>();
        //给数组中添加元素
        list_map.add(maps);
        list_map.add(maps1);

        System.out.println(list_map);
        //给域中添加数据
        model.addAttribute("list_map" , list_map);

        //返回视图名称
        return "eachMap";

    }



    //ifunless
    @GetMapping("/ifunless")
    public String ifunless(Model model){
        //往请求域中添加数据
        model.addAttribute("sex" , "m");
        model.addAttribute("isLogin" , true);
        model.addAttribute("age" , 20);
        model.addAttribute("name" , "");
        model.addAttribute("isOld" , null);
        return "ifunless";
    }


    //switch
    @GetMapping("/switch")
    public String doSwitch(Model model){
        //给请求域中设置值
        model.addAttribute("sex" , "m");
        return "switch";
    }


    //内联text
    @GetMapping("/inline")
    public String inline(Model model){
        model.addAttribute("sex" , "m");
        model.addAttribute("age" , 20);
        model.addAttribute("name" , "张三");
        model.addAttribute("myStudent" , new Student(1005 , "徐小凤" , "女" , 50));
        return "inline";
    }


    //字面量
    @GetMapping("/text")
    public String text(Model model){
        //给请求域中添加数据,给模版使用
        model.addAttribute("sex" , "m");
        model.addAttribute("age" , 20);
        model.addAttribute("name" , "张三");
        model.addAttribute("city" , "北京");
        model.addAttribute("isLogin" , true);
        model.addAttribute("myStudent" , null);
        return "text";
    }

    @GetMapping("/strjoin")
    public String strjoin(Model model) {
        model.addAttribute("sex", "m");
        model.addAttribute("age", 20);
        model.addAttribute("name", "张三");
        model.addAttribute("city", "北京");
        model.addAttribute("isLogin", true);
        model.addAttribute("myStudent", new Student(1001, "邻居几", "男", 22));
        //返回模版名称
        return "strjoin";
    }


    @GetMapping("/sym")
    public String sym(Model model){
        model.addAttribute("sex" , "m");
        model.addAttribute("age" , 20);
        model.addAttribute("name" , "刘伟");
        model.addAttribute("city" , "上海");
        model.addAttribute("isLogin" , true);
        model.addAttribute("myStudent" , new Student(1001 , "长江lisi" ,"男" , 23));
        return "sym";
    }


    //模版内置对象
    @GetMapping("/baseObject")
    public String baseObject(Model model , HttpServletRequest request , HttpSession session){
        //添加数据
        model.addAttribute("myname" , "李四");
        //给request作用域中添加数据
        request.setAttribute("requestData" , "request作用域中的数据");
        //给session作用域中添加数据
        request.getSession().setAttribute("sessionData" , "session作用域中的数据");
        //直接使用session
        session.setAttribute("loginname" , "lisi");

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("one" , "one");
        hashMap.put("two" , "two");
        model.addAttribute("hashMap" , hashMap);
        return "baseObject";
    }


    //内置工具类对象
    @GetMapping("/utilObject")
    public String utilObject(Model model){
        model.addAttribute("mydate" , new Date());
        model.addAttribute("mynum" , 26.695);
        model.addAttribute("mystr" , "bjpowernode");
        //创建一个List
        List<String> list = Arrays.asList("a" , "b" , "c");
        //将list对象添加到请求域数据中
        model.addAttribute("list" , list);
        //创建一个Zoo对象
        Zoo zoo = new Zoo();
        Cat cat = new Cat();
        cat.setName("汤姆");
        Dog dog = new Dog();
        dog.setName("黑娃");
        zoo.setCat(cat);
//        zoo.setDog(dog);
        model.addAttribute("zoo" , zoo);
        return "utilObject";
    }


    @GetMapping("/customtpl")
    public String customtpl(){
        return "customtpl";
    }


















}
